package com.homwee.factory.tvtool;

import com.homwee.factory.R;

import android.content.Context;
import android.graphics.PixelFormat;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.homwee.factory.tvtool.StaticValues;

public class ResultInfoDialog {
	Context mContext;

	WindowManager mWindowManager;
	WindowManager.LayoutParams mLayoutParams;
	LayoutInflater mLayoutInflater;

	LinearLayout mLayout;

	TextView mTvMessage;
	Button mBtOK;

	public ResultInfoDialog(Context cxt,String strMsg) {
		super();
		mContext = cxt;

		mWindowManager = (WindowManager) mContext
				.getSystemService(mContext.WINDOW_SERVICE);
		mLayoutInflater = LayoutInflater.from(mContext);
		mLayoutParams = new WindowManager.LayoutParams();
		mLayoutParams.type = LayoutParams.TYPE_PHONE;
		mLayoutParams.format = PixelFormat.RGBA_8888;
		mLayoutParams.gravity = Gravity.CENTER;
		/*
		 * mLayoutParams.x = 0; mLayoutParams.y = 0;
		 */
		mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;

		mLayout = (LinearLayout) mLayoutInflater.inflate(R.layout.result_info_dialog,
				null);
		mTvMessage = (TextView) mLayout.findViewById(R.id.tv_message);
		mTvMessage.setText(strMsg);
		mBtOK = (Button) mLayout.findViewById(R.id.btOK);

		mBtOK.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				hide();
				if(StaticValues.bSaveAQAndExit)
				{
					mWindowManager.removeView(StaticValues.aqLayoutInstance);
					StaticValues.aqServiceInstance.stopSelf();
				}
				
//				if(StaticValues.bSavePQAndExit)
//				{
//					mWindowManager.removeView(StaticValues.pqLayoutInstance);
//					StaticValues.pqServiceInstance.stopSelf();
//				}
				
			}
		});
		/*
		 * mListView.setOnKeyListener(new OnKeyListener() {
		 * 
		 * @Override public boolean onKey(View arg0, int keyCode, KeyEvent arg2)
		 * { // TODO Auto-generated method stub if(keyCode ==
		 * KeyEvent.KEYCODE_BACK) { hide(); return true; } return false; } });
		 */

	}
	
	public void show() {
		mWindowManager.addView(mLayout, mLayoutParams);
		mBtOK.requestFocus();
	}

	public void hide() {
		mWindowManager.removeView(mLayout);
	}

}
